package com.moleq.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.moleq.base.MoleQBaseDialog;
import com.moleq.base.OnResultListener;
import com.moleq.mgpos.widget.NumberPadButton;
import com.moleq.mgpos.widget.NumberPadForPayment;
import com.moleq.mgpos.widget.NumberPadForPayment.NumberPadListener;
import com.moleq.models.FunctionPanel;
import com.moleq.mgpos.activities.R;
import com.moleq.util.StringUtils;

public class ItemDiscountManualDialog extends MoleQBaseDialog {

	private Button btnConfirm;
	private Button btnClose;
	private NumberPadForPayment mNumPad;
	private int type;
	
	private Button.OnClickListener mOnClickListener = new Button.OnClickListener() {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			switch (arg0.getId()) {
			case R.id.buttonItemDisntClose:
				onCloseWithReturn("");
				break;
			case R.id.buttonItemDisntConfirm:
				String strValue = mNumPad.getBuffer();
				
				if (strValue != null && !strValue.equals("") && !strValue.equals(".")) {
					double dValue = Double.valueOf(strValue);
					if(type == FunctionPanel.ITEM_DISCOUNT_MANUAL_AMOUNT){
						
						dValue = StringUtils.toDouble(strValue);
						if (dValue <= 0) {
							MessageBox.show(mContext, mContext.getResources().getString(R.string.msg_not_zero_amount));
							return;
						}
					}
					if (type == FunctionPanel.ITEM_DISCOUNT_MANUAL_PERCENT && dValue > 100 ) {
						MessageBox.show(mContext, mContext.getResources().getString(R.string.msg_not_more_hundred_percent));
						return;
					}
					
					if (type == FunctionPanel.ITEM_DISCOUNT_MANUAL_PERCENT  && dValue <= 0) {
						MessageBox.show(mContext, mContext.getResources().getString(R.string.msg_not_zero_percent));
						return;
					}
					
					onReturnValue(R.id.buttonItemDisntConfirm,dValue);
					dismiss();
				}
				break;
			}
		}

	};
	
	private NumberPadListener mNumPadListener = new NumberPadListener(){

		@Override
		public void onClick(NumberPadButton button) {

			
		}
		
	};
	private TextView txt_Item_discount;
	
	
//	public void onCloseWithReturn(Object obj){
//		mOnResultListener.onReturnValue(DIALOG_INPUT_CHECK_INFO,obj);
//		dismiss();
//	}


	public ItemDiscountManualDialog(Context context, Object base) {
		super(context,R.style.dialog,base);
		constructor(context,base);
	}
	
	public ItemDiscountManualDialog(Context context, int theme,Object base) {
		super(context, theme,base);
		constructor(context,base);
	}
	
	private void constructor(Context context,Object base) {
		mContext = context;
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.dialog_perview_item_discount);
		this.setCancelable(false);

		btnConfirm = (Button) findViewById(R.id.buttonItemDisntConfirm);
		btnConfirm.setOnClickListener(mOnClickListener);

		btnClose = (Button) findViewById(R.id.buttonItemDisntClose);
		btnClose.setOnClickListener(mOnClickListener);

		mNumPad = (NumberPadForPayment) findViewById(R.id.ItemDisntNumPad);
		mNumPad.setNumberPadListener(mNumPadListener);

		
		txt_Item_discount = (TextView) findViewById(R.id.txt_Item_discount);
	}
	
	@Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();
		
		if (type == FunctionPanel.ITEM_DISCOUNT_MANUAL_PERCENT) {
			txt_Item_discount.setText("Percent(%)");
		}else if (type == FunctionPanel.ITEM_DISCOUNT_MANUAL_AMOUNT) {
			txt_Item_discount.setText("Amount");
		}
		
		mNumPad.reset();
		mNumPad.requestFocusFromTouch();
		mNumPad.setStyle(NumberPadForPayment.NO_ENTER_STYLE);
		
	}

	public void setMode(int itemDiscountManualType) {
		// TODO Auto-generated method stub
		type = itemDiscountManualType;
	}
	
}
